﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.DeliveryForms
{
    public class AttachDeliveryDocumentCommand : IBaseCommand
    {

        #region private methods

        byte[] _deliveryFormContent;
        int _deliveryFormItemId;
        string _deliveryFormName;

        #endregion

        #region constructors

        public AttachDeliveryDocumentCommand(byte[] deliveryFormCotent, int deliveryFormItemId, string deliveryFormName)
        {
            _deliveryFormContent = deliveryFormCotent;
            _deliveryFormItemId = deliveryFormItemId;
            _deliveryFormName = deliveryFormName;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {            
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentSite.AllowUnsafeUpdates = true;
                        currentWeb.AllowUnsafeUpdates = true;
                        SPListItem deliveryFormItem = currentWeb.Lists[GlobalConstants.LIST_PLANILLAS].GetItemById(_deliveryFormItemId);
                        Dictionary<string,byte[]> deliveryFile = new Dictionary<string,byte[]>();
                        deliveryFile.Add(_deliveryFormName,_deliveryFormContent);
                        ListHelper.AttachFilesToListItem(deliveryFormItem, deliveryFile);
                        deliveryFormItem.Update();
                        currentSite.AllowUnsafeUpdates = false;
                        currentWeb.AllowUnsafeUpdates = false;
                    }
                }
            });     
        }

        public void Undo()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentSite.AllowUnsafeUpdates = true;
                        currentWeb.AllowUnsafeUpdates = true;
                        SPListItem deliveryFormItem = currentWeb.Lists[GlobalConstants.LIST_PLANILLAS].GetItemById(_deliveryFormItemId);
                        foreach(SPFile currentFile in deliveryFormItem.Attachments)
                        {
                            currentFile.Delete();
                            deliveryFormItem.Update();
                        }
                        currentSite.AllowUnsafeUpdates = false;
                        currentWeb.AllowUnsafeUpdates = false;

                    }
                }
            });
        }

        #endregion
    }
}
